package com.publishproject.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;

import com.nineoldandroids.animation.ObjectAnimator;

import com.nineoldandroids.animation.TypeEvaluator;
import com.nineoldandroids.animation.ValueAnimator;
import com.publishproject.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * @author Android客户端组-tanghongbin
 * @Title: MySurfaceView
 * @Package com.publishproject.test
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/1/11 17:24
 * @Description:
 */
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
    public MySurfaceView(Context context) {
        super(context);
    }

    private MyThread myThread;
    private SurfaceHolder holder;
    private List<Point> points = new ArrayList<>();
    private Handler mHandler = new Handler();

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        holder = getHolder();
        holder.addCallback(this);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        myThread = new MyThread();
        myThread.start();
        Log.i("TAG","线程成功开启");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        try {
            myThread.interrupt();
            Log.i("TAG","线程销毁");
        }catch (Exception e){

        }
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            try {
                while (true){
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                drawCircle();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    sleep(12000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private int generateWidth(){
        return new Random().nextInt(700)+100;
    }
    private int generateHeight(){
        return new Random().nextInt(1000)+100;
    }
    private Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.abc111111);

    private Rect rect = new Rect(0,100,0,1100);
    int i = 100;
    int j = 0;
    int xishu = 1;
    int width = 100;
    int length = 100;
    private void drawCircle() throws Exception{

        final ObjectAnimator animator = new ObjectAnimator();
        animator.setDuration(10000);
        Point start = new Point(100,100);
        Point end = new Point(WindowUtil.getDisplayMetrics().widthPixels-100,WindowUtil.getDisplayMetrics().heightPixels-100);
        animator.setObjectValues(start,end);
        animator.setTarget(end);
        animator.setEvaluator(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                Point start = (Point) startValue;
                Point end = (Point) endValue;
                Log.i("TAG","打印起点x:"+start.getX()+"y:"+start.getY()+"终点x:"+end.getX()+"y:"+end.getY());
                int width = (int) ((end.getX()-start.getX())*fraction);
                int heigiht = (int) ((end.getY()-start.getY())*fraction)+50;
                Point internalPoint = new Point(width,heigiht);
                return internalPoint;
            }
        });
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                try {
                    Point point = (Point) animation.getAnimatedValue();
                    Log.i("TAG","打印point"+point);
                    Canvas canvas = holder.lockCanvas(null);
                    canvas.drawColor(Color.BLACK);
                    Paint paint = new Paint();
                    paint.setColor(Color.parseColor("#ff4966"));
                    canvas.drawCircle(point.getX(),point.getY(),50,paint);
                    holder.unlockCanvasAndPost(canvas);
                }catch (Exception e){
                    animator.cancel();
                    e.printStackTrace();
                }
            }
        });
        animator.start();
    }
}
